package cn.jsc.user.controller;

import cn.jsc.basic.exception.BusinessException;
import cn.jsc.basic.util.AjaxResult;
import cn.jsc.user.dto.LoginDto;
import cn.jsc.user.service.ILoginInfoServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginInfoServiceI loginInfoService;


    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){

        try {
            return loginInfoService.accountLogin(loginDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败, "+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误, "+e.getMessage());
        }

    }
    /**
     * 微信登陆
     * @param map
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> map){

        try {
            String code = map.get("code");
            return loginInfoService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败, "+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误, "+e.getMessage());
        }

    }
    /**
     * 微信绑定
     * @param map
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){

        try {
            //微信绑定
            return loginInfoService.wechatBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败, "+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误, "+e.getMessage());
        }

    }
}
